<?php
try {
	if (file_exists('protected/views/site/cities.php'))
		unlink('protected/views/site/cities.php');
	$file = 'protected/views/site/cities.php';
	
	$sql='SELECT title,latitude,longitude FROM location';
	$connection=Yii::app()->db;
	$command=$connection->createCommand($sql);
	$rows=$command->queryAll();
	
	$beginning="<?php\r\n\$items = array(\r\n";
	$end=");\r\n?>";
	file_put_contents($file, $beginning, FILE_APPEND | LOCK_EX);
	foreach ($rows as $row) {
		$city="\"".$row['title']."\"=>\"".$row['latitude'].",".$row['longitude']."\",\r\n";
		// Write the contents to the file, 
		// using the FILE_APPEND flag to append the content to the end of the file
		// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
		file_put_contents($file, $city, FILE_APPEND | LOCK_EX);
	}
	file_put_contents($file, $end, FILE_APPEND | LOCK_EX);
	echo 'Plik z miastami został wypełniony poprawnie!';
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>